package com.icode.web.controller.rent;

import com.icode.core.dto.house.RentDTO;
import com.icode.core.dto.order.PageDTO;
import com.icode.core.model.house.Rent;
import com.icode.service.house.HouseService;
import com.icode.service.management.RentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lb
 * Date: 15-11-30
 * Time: 上午10:02
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/rentback")
public class RentBackController {

    @Autowired
    private RentService rentService;
    @Autowired
    private HouseService houseService;

    @RequestMapping(value = "showall/{currentPage}/{status}", method = RequestMethod.GET)
    private String showall(@PathVariable String currentPage, @PathVariable String status, Model model) {
        String totalPage = houseService.findTotalPageByStatus(Rent.class, status).toString();
        currentPage = PageDTO.isPage(currentPage, totalPage);
        List<RentDTO> rentDTOs = rentService.findRentAllByPage(currentPage, status);
        model.addAttribute("rentDTOs", rentDTOs);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        model.addAttribute("status", status);
        return "back/rent/rentList";
    }

    @RequestMapping(value = "rentdetails/{rentguid}", method = RequestMethod.GET)
    private String rentdetails(@PathVariable String rentguid, Model model) {
        RentDTO rentDTO = rentService.findRentByGuid(rentguid);
        model.addAttribute("rentDTO", rentDTO);
        return "back/rent/rentDetails";
    }

    @RequestMapping(value = "searchRentOut/{username}", method = RequestMethod.GET)
    @ResponseBody
    private List<RentDTO> searchRentOut(@PathVariable String username) {
        List<RentDTO> rentDTOs = rentService.findRentByUsername(username);
        return rentDTOs;
    }

    @RequestMapping(value = "searchRentIn/{customer}", method = RequestMethod.GET)
    @ResponseBody
    private List<RentDTO> searchRentIn(@PathVariable String customer){
        List<RentDTO> rentDTOs = rentService.findRentByCustomer(customer);
        return rentDTOs;
    }
}
